package itypes

import (
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/logger"
	"github.com/hwcer/updater"
)

var Collect = NewCollectIType(config.ITypeCollect)
var CollectAchievementBall = NewCollectIType(config.ITypeAchievementBall)
var CollectAchievementPacket = NewCollectIType(config.ITypeAchievementPacket)
var CollectNickName = NewCollectIType(config.ITypeNickName)
var CollectHeadIcon = NewCollectIType(config.ITypeHeadIcon)
var CollectHeadFrame = NewCollectIType(config.ITypeHeadFrame)
var CollectBusinessCard = NewCollectIType(config.ITypeBusinessCard)
var CollectAppBackground = NewCollectIType(config.ITypeAppBackground)
var CollectPlayerCloth = NewCollectIType(config.ITypePlayerCloth)

var CollectUnit = NewCollectIType(config.ITypeCollectUnit)
var CollectChip = NewCollectIType(config.ITypeCollectChip)

func init() {
	arr := []updater.IType{
		Collect, CollectAchievementBall,
		CollectAchievementPacket, CollectNickName,
		CollectHeadIcon, CollectHeadFrame,
		CollectBusinessCard, CollectAppBackground,
		CollectPlayerCloth, CollectUnit, CollectChip,
	}

	im := &model.Collect{}
	if err := updater.Register(updater.ParserTypeCollection, updater.RAMTypeNone, im, arr...); err != nil {
		logger.Panic(err)
	}
}

func NewCollectIType(it int32) *CollectIType {
	r := &CollectIType{}
	r.IType = *NewIType(it)
	r.IType.SetCreator(collectCreator)
	return r
}

type CollectIType struct {
	IType
}

func collectCreator(u *updater.Updater, iid int32, val int64) (any, error) {
	var err error
	i := &model.Collect{}
	i.Model.Init(u, iid)
	i.OID = model.Unique(u, iid)
	i.Val = int32(val)
	i.Bag = i.IType(iid)
	i.Create = u.Unix()
	i.Update = i.Create

	return i, err
}
